/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Principal.java
 *
 * Created on 27-oct-2011, 18:03:00
 */
package UI;

import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import modela.Carro;
import modela.Fondo;
import modela.Peaton;

/**
 *
 * @author Witchy
 */
public class PrincipalApp extends javax.swing.JFrame {

    private Icon Carro1, Carro2, Hombre, Mujer, Fondo0;//Icons utilizadas para cargar las imagenes
    public static final int Dimension_Imagenes = 32;//Indica la dimensión de las imagenes. Puede ser de 32 y 48
    public static int mNumeroCarros, mNumeroCocineros, mNumeroPersonas, mNumeroServidores, mTiempoColaPersonas;
    public static int mTiempoRecibirPersonas, mTiempoColaCarros, mTiempoRecibirCarros, mTiempoMesas , mTiempoTotal;

    JLayeredPane panel_fondo;

    /** Creates new form Principal */
    public PrincipalApp(int pNumeroCarros, int pNumeroCocineros, int pNumeroPersonas, int pNumeroServidores,
            int pTiempoColaPersonas, int pTiempoRecibirPersonas, int pTiempoColaCarros, int pTiempoRecibirCarros,
            int pTiempoMesas,int pTiempoTotal) {
        initComponents();

        mNumeroCarros = pNumeroCarros;
        mNumeroCocineros = pNumeroCocineros;
        mNumeroPersonas = pNumeroPersonas;
        mNumeroServidores = pNumeroServidores;
        mTiempoColaPersonas = pTiempoColaPersonas;
        mTiempoRecibirPersonas = pTiempoRecibirPersonas;
        mTiempoColaCarros = pTiempoColaCarros;
        mTiempoRecibirCarros = pTiempoRecibirCarros;
        mTiempoMesas = pTiempoMesas;
        mTiempoTotal = pTiempoTotal;



        panel_fondo= new JLayeredPane();
        panel_fondo.setBounds(20,0,662,597);
        panel_fondo.setVisible(true);
        add(panel_fondo);
        Fondo fondo = new Fondo();
        panel_fondo.add(fondo.getFondo(),new Integer(1));

        jLabel1.setVisible(false);

      

        Carro1 = (Icon) loadIcon("IMG/car1_" + Dimension_Imagenes + ".png");
        Carro2 = (Icon) loadIcon("IMG/car2_" + Dimension_Imagenes + ".png");
        Hombre = (Icon) loadIcon("IMG/man_" + Dimension_Imagenes + ".png");
        Mujer = (Icon) loadIcon("IMG/woman_" + Dimension_Imagenes + ".png");

//jLabel1.setIcon(Carro1);
        for (int i = 0; i < mNumeroCarros; i++) {

            this.AgregarCarro(400, 50);

        }

        for (int i = 0; i < mNumeroCocineros; i++) {
            
        }

        for (int i = 0; i < mNumeroPersonas; i++) {

            this.AgregarPersona(454, 456);
        }

        for (int i = 0; i < mNumeroServidores; i++) {
        }
        
for(int i=0;i<this.getComponentCount(); i++){
    System.out.println(getComponentZOrder(this.getComponent(i)) + this.getComponent(i).getName());
}

    }

    /**
     * Crea una instancia de Carro y agrega su JLabel al formulario
     * @param x Coordenada inicial en X de la imagen
     * @param y Coordenada inicial en Y de la imagen
     * @return Nueva instancia de Carro
     */
    public Carro AgregarCarro(int x, int y) {
        Icon aux = null;
        if ((((int) (Math.random() * 100)) % 2) == 0) {
            aux = Carro2;
        } else {
            aux = Carro1;
        }

        Carro nuevo = new Carro(aux, x, y);
        panel_fondo.add(nuevo.Imagen,new Integer(2));
        nuevo.start();

        return nuevo;
    }

    /**
     * Crea una instancia de Persona y agrega su JLabel al formulario
     * @param x Coordenada inicial en X de la imagen
     * @param y Coordenada inicial en Y de la imagen
     * @return  Nueva instancia de Persona
     */
    public Peaton AgregarPersona(int x, int y) {
        Icon aux = null;
        if ((((int) (Math.random() * 100)) % 2) == 0) {
            aux = Hombre;
        } else {
            aux = Mujer;
        }

        Peaton nuevo = new Peaton(aux, x, y);
        panel_fondo.add(nuevo.Imagen,new Integer(2));
        nuevo.start();

        return nuevo;
    }

    /**
     * Método privado que obtiene imagenes a partir de los recursos
     * @param nombre Nombre del recurso
     * @return ImageIcon con del recurso solicitado, o <i>null</i> si no se encuentra el recurso.
     */
    private ImageIcon loadIcon(String nombre) {
        ImageIcon icono;
        URL url = null;
        try {
            url = getClass().getClassLoader().getResource(nombre);
            icono = new ImageIcon(url);
            if (icono == null) {
                System.err.println("BASURA");
            }
            return icono;
        } catch (Exception e) {
            System.out.println("No se pudo cargar la imagen " + nombre + " de " + url);
            System.out.println("El error fue : " + e.getClass().getName() + " " + e.getMessage());
            System.exit(0);
            return null;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(250, 0), new java.awt.Dimension(250, 0), new java.awt.Dimension(250, 32767));
        filler4 = new javax.swing.Box.Filler(new java.awt.Dimension(50, 0), new java.awt.Dimension(50, 0), new java.awt.Dimension(50, 32767));
        filler3 = new javax.swing.Box.Filler(new java.awt.Dimension(100, 0), new java.awt.Dimension(100, 0), new java.awt.Dimension(100, 32767));
        jButton1 = new javax.swing.JButton();
        filler2 = new javax.swing.Box.Filler(new java.awt.Dimension(50, 0), new java.awt.Dimension(50, 0), new java.awt.Dimension(50, 32767));
        jButton2 = new javax.swing.JButton();
        jToolBar2 = new javax.swing.JToolBar();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Simulacion"); // NOI18N
        setResizable(false);

        jToolBar1.setRollover(true);
        jToolBar1.add(filler1);
        jToolBar1.add(filler4);
        jToolBar1.add(filler3);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/Reproducir.png"))); // NOI18N
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton1);
        jToolBar1.add(filler2);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/Detener.png"))); // NOI18N
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton2);

        jToolBar2.setRollover(true);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/Agregar.png"))); // NOI18N
        jButton3.setToolTipText("Agregar Parametros");
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jToolBar2.add(jButton3);

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/Ayuda.png"))); // NOI18N
        jButton4.setToolTipText("Ayuda");
        jButton4.setFocusable(false);
        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar2.add(jButton4);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/linea1.png"))); // NOI18N

        jMenu1.setText("Archivo");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Nuevo");
        jMenu1.add(jMenuItem1);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Agregar Parametros");
        jMenu1.add(jMenuItem2);

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem3.setText("Simular");
        jMenu1.add(jMenuItem3);

        jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem4.setText("Salir");
        jMenu1.add(jMenuItem4);

        jMenuBar1.add(jMenu1);

        jMenu3.setText("Simulador");

        jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem5.setText("Correr");
        jMenu3.add(jMenuItem5);

        jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem6.setText("Detener");
        jMenu3.add(jMenuItem6);

        jMenuBar1.add(jMenu3);

        jMenu2.setText("Ayuda");

        jMenuItem7.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem7.setText("Ayuda");
        jMenu2.add(jMenuItem7);

        jMenuItem8.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem8.setText("Acerca De..");
        jMenu2.add(jMenuItem8);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 953, Short.MAX_VALUE)
            .addComponent(jToolBar2, javax.swing.GroupLayout.DEFAULT_SIZE, 953, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(167, 167, 167)
                .addComponent(jLabel1)
                .addContainerGap(765, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jButton3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PrincipalApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PrincipalApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PrincipalApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PrincipalApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PrincipalApp(mNumeroCarros, mNumeroCocineros, mNumeroPersonas, mNumeroServidores,
                        mTiempoColaPersonas, mTiempoRecibirPersonas, mTiempoColaCarros, mTiempoRecibirCarros,
                        mTiempoMesas, mTiempoTotal).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.Box.Filler filler1;
    private javax.swing.Box.Filler filler2;
    private javax.swing.Box.Filler filler3;
    private javax.swing.Box.Filler filler4;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    // End of variables declaration//GEN-END:variables
}
